<?php
/**
 * 土特产商品
 *
 * @author xue
 */
namespace Ttc\Controller;
use Ttc\Controller\PublicController;
class GoodsController extends PublicController {
    /**
     * 发布土特产
     */
    public function addGoods(){
        $data = inputJson();

        // $data['phpsid'] = 'bqodcvt6gbl9k700ql2afqifr4';
        // $data['goods_validity'] = '1';
        // $data['goods_name'] ='hhh';
        // $data['goods_price'] = 3;
        // $data['goods_deliver'] = '1';
        // $data['goods_type'] = '2';
        // $data['goods_desc'] = '333';
        // $data['contact_id'] = '1';
        // $data['images_id'] = '33';
        // $data['goods_add_place'] = '33';
        // $data['long'] = '33'; //经度
        // $data['lat'] = '33'; //纬度
        // $data['token'] = 'k90SNk43'; //纬度

        //检查令牌
        $token = $this->validataToken($data['token'],$data['phpsid']);//成功返回新的令牌 接口调用末尾返回给前端
        if($token == -2){
            errorOutputJson('d'.__LINE__);//没有初始化令牌
        }elseif ($token == -3) {
            errorOutputJson('d'.__LINE__);//存储新令牌出错
        }elseif ($token == -1) {
            abnormalOutputJson(['info'=>'请求太快','type'=>2]);
        }

        //是否登录
        $res = is_login($data['phpsid']);
        if(!$res){
            abnormalOutputJson(['info'=>'请先登录','type'=>1]);
        }

        //判断传过来的有效天数是不是数字
        // $data['goods_validity'] = '2';//不能传0，空，页2好，可以2元
        if(!intval($data['goods_validity'])){
            abnormalOutputJson(['info'=>'请输入正确的有效天数']);
        }else{
            //发布日期
            $good_data['goods_add_time'] = time();
            //失效日期
            $good_data['goods_expire_time'] = strtotime('+'.intval($data['goods_validity']).' day');
        }

        //获取用户id
        $member_id =get_userId($data['phpsid']);

        $good_data['member_id'] = $member_id;
        $good_data['goods_name'] = $data['goods_name'];
        $good_data['goods_price'] = $data['goods_price'];
        $good_data['goods_deliver'] = rtrim($data['goods_deliver'],',');
        $good_data['goods_type'] = rtrim($data['goods_type'],',');
        $good_data['goods_desc'] = $data['goods_desc'];
        $good_data['contact_id'] = $data['contact_id'];
        $good_data['goods_add_place'] = $data['goods_add_place'];
        $good_data['longitude'] = $data['long'];
        $good_data['latitude'] = $data['lat'];
        $good_data['images_id'] = rtrim($data['images_id'],',');

        //添加商品
        $TtcGoods = D('TtcGoods');
        $TtcGoods->sx_data = $good_data;
        $res = $TtcGoods->addGoods();
        if(isset($res['info'])){
            abnormalOutputJson(['info'=>$res['info']]);
        }elseif(!$res){
            errorOutputJson('a'.__LINE__);
        }else{
            successOutputJson($res,['token'=>$token]);
        }
    }
    /**
     * 土特产商品详情
     */
    public function GoodsInfo(){
        $data = inputJson();

        ini_set('max_execution_time', '5');

        //商品id
        // $data['id'] = '100542';

        if(empty($data['id'])){
            abnormalOutputJson(['info'=>'商品不存在']);
        }

        //查询商品
        $TtcGoods = D('TtcGoods');
        $res = $TtcGoods->GoodsInfo($data['id']);
        // var_dump($res);

        if($res === false){
            errorOutputJson('a'.__LINE__);
        }elseif(empty($res)){
            abnormalOutputJson(['info'=>'商品不存在']);
        }else{
            //查session_id
            // var_dump($res['member_id']);//member_id
            $rres = D('TtcMember')->find($res['member_id']);
            $res['sid'] = $rres['session_id'];
            successOutputJson($res);
        }

    }
    /**
     * 马上联系 商品联系人
     */
    public function GoodsContacts(){

        $data = inputJson();

        // var_dump($data);
        // $data['phpsid']='bqodcvt6gbl9k700ql2afqifr4';//当前操作用户session_id
        // $data['sid']='bqodcvt6gbl9k700ql2afqifjj';//当前商品的商家session_id
        // $data['goods_id']=100537;


        $state = is_login($data['phpsid']);
        if($state == 0){
            abnormalOutputJson(['info'=>'请先登录']);
        }

        // $data['goods_id'] = 1;//商品id
        //获取商品用户id
        $member_id =get_userId($data['sid']);
        //获取操作用户id
        $cmember_id =get_userId($data['phpsid']);

        $TtcGoods = D('TtcGoods');
        //增加联系次数 不是本人操作的才增加联系次数 判断如果是已经联系过的人不再增加次数
        $ActiveContact =D('ActiveContact');
        $is_contact = $ActiveContact->getAcontactInfo(array('goods_id'=>$data['goods_id'],'member_id'=>$cmember_id));
        if(empty($is_contact) && ($data['phpsid'] != $data['sid'])){
            $cre = $TtcGoods->saveGoodsClick($data['goods_id']);
            if($cre === false){
                errorOutputJson('a'.__LINE__);
            }else{
                //添加联系过商品记录
                $adata['goods_id'] = $data['goods_id'];
                $adata['member_id'] = $member_id;
                $res = $ActiveContact->add($adata);
                if(!$res){
                    errorOutputJson('a'.__LINE__);
                }
            }
        }

        //查联系id
        $condition = array('member_id'=>$member_id,'id'=>$data['goods_id']);
        $field = 'contact_id';
        $res = $TtcGoods->getOneGoods($condition,$field);
        if($res === false){
            errorOutputJson('a'.__LINE__);
        }elseif(empty($res['contact_id'])){
            abnormalOutputJson(['info'=>'没有联系方式']);
        }else{
            $contact_id = $res['contact_id'];
            //查询商品联系人
            $Contact = D('Contact');
            $res = $Contact->getOneContact(array('id'=>$contact_id),'contact_name,contact_phone,contact_wechat');
            if($res === false){
                errorOutputJson('a'.__LINE__);
            }else{
                successOutputJson($res);
            }
        }
        
    }

    /**
     * 土特产列表
     */
    public function GoodsLists(){

        //查询土特产列表
        $data = inputJson();

        // $data['phpsid'] = 'veu8ds0l22dl6868kj140kspu2';
        // $data['page'] = 1;
        $data['rows'] = 10;
        // $data['lati'] = 10000;
        // $data['long'] = 10000;
        // $data['share'] = 1;


        //如果是从分享进来，则不用判断有没有登录
        if($data['share'] == 1){
            $state = is_login($data['phpsid']);
            if($state == 0){
                abnormalOutputJson(['info'=>'商家不存在','type'=>2]);
            }
        }else{
            $state = is_login($data['phpsid']);
            if($state == 0){
                abnormalOutputJson(['info'=>'请先登录','type'=>1]);
            }
        }

        //获取用户id
        $member_id =get_userId($data['phpsid']);
        //查商店名称
        $store_name = D('TtcMember')->getMemberInfo(array('id'=>$member_id),'store_name');

        $good_list = array();
        $good_list['member_id'] = $member_id;
        $good_list['page'] = $data['page'];
        $good_list['rows'] = $data['rows'];
        $good_list['lati'] = $data['lati'];
        $good_list['long'] = $data['long'];

        $TtcGoods = D('TtcGoods');
        //把商品24h后没有审核的状态改为未审核
        $updata['is_examine'] = 3;//未审核
        $condition = array('goods_add_time'=>['LT',time()-C('EXPIRY_AUDIT_TIME')],'is_examine'=>['EQ',0]);
        $ures = $TtcGoods->saveGoods($condition,$updata);
        if($ures === false){
            errorOutputJson('a'.__LINE__);
        }

        //本商家操作要显示所有商品，分享进来的话不要显示审核失败或未审核的商品
        if($data['share'] == 1){
            $res = $TtcGoods->GoodsLists($good_list,1);
        }else{
            $res = $TtcGoods->GoodsLists($good_list);
        }

        if($res === false){
            errorOutputJson('a'.__LINE__);
        }elseif(empty($res['data'])){
            successOutputJson([],['all_page'=>$res['all_page'],'store_name'=>$store_name['store_name']]);
        }else{
            successOutputJson($res['data'],['all_page'=>$res['all_page'],'store_name'=>$store_name['store_name']]);
        }
    }

    /**
     * 转发量+1
     */
    public function GoodsForward(){
        $data = inputJson();

        // $data['phpsid']='bqodcvt6gbl9k700ql2afqifr4';
        // $data['goods_id']=100511;

        $state = is_login($data['phpsid']);
        if($state == 0){
            abnormalOutputJson(['info'=>'请先登录']);
        }

        //增加转发量
        $TtcGoods = D('TtcGoods');
        $res = $TtcGoods->saveGoodsForw($data['goods_id']);

        if($res === false){
            errorOutputJson('a'.__LINE__);
        }else{
            successOutputJson();
        }

    }
    /**
     * 保存投诉
     */
    public function GoodsComplaint(){
        $data = inputJson();

        // $data['phpsid']='bqodcvt6gbl9k700ql2afqifr4';
        // $data['goods_id']=100511;
        // $data['content']='投诉';
        // $data['imgs_id']='1';

        $cdata['goods_id']=$data['goods_id'];
        $cdata['content']=$data['content'];
        $cdata['imgs_id']=$data['imgs_id'];

        $state = is_login($data['phpsid']);
        if($state == 0){
            abnormalOutputJson(['info'=>'请先登录']);
        }

        if(empty($data['goods_id'])){
            abnormalOutputJson(['info'=>'商品不存在']);
        }

        $cdata['member_id'] =get_userId($data['phpsid']);
        // var_dump($member_id);

        //保存投诉
        $Complaint = D('Complaint');
        $res = $Complaint->addComplaint($cdata);
        if(!$res){
            errorOutputJson('a'.__LINE__);
        }elseif(isset($res['msg'])){
            abnormalOutputJson(['info'=>$res['info']]);
        }else{
            successOutputJson();
        }

    }

}